'use strict';

const {
  PACKAGE_LIST_CATCE_KEY,
} = require('../../../../config/redisKey');

exports.index = async ctx => {
  const resData = await ctx.getListCache(PACKAGE_LIST_CATCE_KEY, async () => {
    ctx.validate({
      type: {
        required: false,
        type: 'string',
        default: 'all',
      },
      searchKey: {
        required: false,
        type: 'string',
      },
      searchType: {
        required: false,
        convertType: 'int',
        type: 'int',
        max: 3,
        min: 0,
      },
    }, ctx.query);
    ctx.w_validate_page();
    const data = await Promise.all([ ctx.service.package.list(), ctx.service.package.count() ]);
    return {
      list: data[0],
      total: data[1],
      pageSize: ctx.query.pageSize,
      pageNum: ctx.query.pageNum,
    };
  })
  ctx.sendSuccess(resData);
};

exports.show = async ctx => {
  ctx.sendSuccess(await ctx.service.package.detail());
};
